package com.lemurian.bankdata.lite.struts;

public class Pager {
	static long totalpage,  currentpage;
    private static long maxpagepersection = 10;
    private static int maxitemperpage = 50;

    /**
     * Returns the pager.
     * @return String
     */
    public static String generatePager(int start, int count, long total) {
        if (count > 0) {
            maxitemperpage = count;
        }
        total = total - 1;
        if (total < 0) {
            total = 0;
        }
        String pager = "";
        totalpage = (total + maxitemperpage) / maxitemperpage;
        currentpage = (start + maxitemperpage) / maxitemperpage;
        pager = "<div style='float: left' class='dataTables_info'> Page <b>" + currentpage + "</b> of " + totalpage + " pages. <div>&nbsp;";
        return pager;
    }

    /**
     * Returns the pagerItem.
     * @return String
     */
    public static String generatePagerItem(int start, int count, long total) {
        if (count > 0) {
            maxitemperpage = count;
        }
        total = total - 1;
        if (total < 0) {
            total = 0;
        }
        int prev, next;
        String pagerItem = "";
        totalpage = (total + maxitemperpage) / maxitemperpage;
        currentpage = (start + maxitemperpage) / maxitemperpage;
        if (start == 0) {
            prev = 0;
        } else {
            prev = start - maxitemperpage;
        }
        next = start + maxitemperpage;
        pagerItem = pagerItem + "<div class='dataTables_paginate paging_full_numbers' style='float: right'>";
        if (start >= maxitemperpage) {
        	pagerItem = pagerItem + "<a  href=javascript:page(" + 0 + "," + maxitemperpage + ") class='first paginate_button'>First</a>";
        	pagerItem = pagerItem + "<a href=javascript:page(" + prev + "," + maxitemperpage + ") class='previous paginate_button'>Previous</a>";
        } else {
        	pagerItem = pagerItem + "<a disabled='true' href='#' class='first paginate_button'>First</a>";
        	pagerItem = pagerItem + "<a disabled='true' href='#' class='previous paginate_button'>Previous</a>";
        }
        long z;
        long sectionstart = currentpage - maxpagepersection / 2;
        if (sectionstart + maxpagepersection - 1 > totalpage) {
            sectionstart = sectionstart - (sectionstart + maxpagepersection - 1 - totalpage);
        }
        if (sectionstart < 1) {
            sectionstart = 1;
        }
        
        pagerItem = pagerItem + "<span>";
        
        for (z = sectionstart; (z < sectionstart + maxpagepersection) && (z <= totalpage); z++) {
            if (z == currentpage) {
            	pagerItem = pagerItem + "<a disabled='true' class='paginate_active'>" + currentpage + "</a>";
            } else {
            	pagerItem = pagerItem + "<a href=javascript:page(" + (z - 1) * maxitemperpage + "," + maxitemperpage + ") class='paginate_button'>" + z + "</a>";
            }
        }
        
        pagerItem = pagerItem + "</span>";
        
        if (currentpage < totalpage) {
        	pagerItem = pagerItem + "<a href=javascript:page(" + next + "," + maxitemperpage + ") class='next paginate_button'>Next</a>";
        	pagerItem = pagerItem + "<a href=javascript:page(" + ((totalpage - 1) * maxitemperpage) + "," + maxitemperpage + ") class='last paginate_button' >Last</a>";
        } else {
        	pagerItem = pagerItem + "<a disabled='true' href='#' class='next paginate_button'>Next</a>";
        	pagerItem = pagerItem + "<a disabled='true' href='' class='last paginate_button' >Last</a>";
        }
        pagerItem = pagerItem + "</div>";
        return pagerItem;
    }
}